package logic

import (
	"context"
	"net/http"

	"bigmodel/internal/svc"
	"bigmodel/internal/types"

	"github.com/fumiama/go-docx"
	"github.com/zeromicro/go-zero/core/logx"
)

type GeneFileLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGeneFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GeneFileLogic {
	return &GeneFileLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GeneFileLogic) GeneFile(req *types.UserInfo, rw http.ResponseWriter) error {
	w := docx.New().WithDefaultTheme()

	// add new paragraph
	para1 := w.AddParagraph().Justification("center")
	// add text
	run := para1.AddText("标题").Size(req.TitleParam.Size).Color(req.TitleParam.Color).Highlight(req.TitleParam.Background).Bold()
	run.RunProperties.Spacing = &docx.Spacing{
		LineRule: "exact",
		Before:   req.TitleParam.Spacing,
	}
	para2 := w.AddParagraph()
	run = para2.AddText("用户名:" + req.Username).Size(req.ContentParam.Size).Color(req.ContentParam.Color).Highlight(req.ContentParam.Background)
	run.RunProperties.Spacing = &docx.Spacing{
		LineRule: "exact",
		Before:   req.ContentParam.Spacing,
	}
	
	para3 := w.AddParagraph()
	run = para3.AddText("手机号:" + req.Mobile).Size(req.ContentParam.Size).Color(req.ContentParam.Color).Highlight(req.ContentParam.Background)
	run.RunProperties.Spacing = &docx.Spacing{
		LineRule: "exact",
		Before:   req.ContentParam.Spacing,
	}
	para4 := w.AddParagraph()
	run = para4.AddText("描述:" + req.Description).Size(req.ContentParam.Size).Color(req.ContentParam.Color).Highlight(req.ContentParam.Background)
	run.RunProperties.Spacing = &docx.Spacing{
		LineRule: "exact",
		Before:   req.ContentParam.Spacing,
	}
	para5 := w.AddParagraph()
	run = para5.AddText("简介:" + req.Appeal).Size(req.ContentParam.Size).Color(req.ContentParam.Color).Highlight(req.ContentParam.Background)
	run.RunProperties.Spacing = &docx.Spacing{
		LineRule: "exact",
		Before:   req.ContentParam.Spacing,
	}
	// f, err := os.Create("/Users/liangzhihao/companyCode/test/bigmodel/internal/text/generated.docx")
	// save to file
	// if err != nil {
	// 	panic(err)
	// }
	// 设置Content-Type
	rw.Header().Set("Content-Type", "application/octet-stream")
	// 设置Content-Disposition
	rw.Header().Set("Content-Disposition", "attachment; filename=user.docx")
	// var file os.File
	_, err := w.WriteTo(rw)

	// 写入文件内容到ResponseWriter
	// _, err := io.Copy(rw)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	}

	return nil
}
